package com.aura.blog.controller;

import com.aura.blog.dto.request.CreateUserRequest;
import com.aura.blog.dto.request.UpdateUserRequest;
import com.aura.blog.dto.request.UserLoginRequest;
import com.aura.blog.dto.response.UserInfoResponse;
import com.aura.blog.dto.response.UserLoginResponse;
import com.aura.blog.entity.UserInfo;
import com.aura.blog.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户相关接口")
public class UserController {

    @Resource(name = "userServiceImpl")
    private UserService userService;

    /**
     * 用户登录接口
     *
     * @param loginRequest 用户登录请求对象，包含用户名和密码
     * @return 用户登录响应对象，包含用户信息和认证令牌
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户通过用户名和密码进行登录认证")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "登录成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "认证失败")
    })
    public UserLoginResponse login(@Validated @RequestBody UserLoginRequest loginRequest) {
        log.info("用户登录, userName: {}", loginRequest.getUserName());
        return userService.check(loginRequest);
    }

    /**
     * 获取用户信息
     *
     * @param userId 用户ID，不能为空
     * @return 用户信息响应对象
     */
    @GetMapping("/getUserInfo")
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户详细信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证"),
        @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public UserInfoResponse getUserInfo(@Parameter(description = "用户ID") @NotNull Integer userId) {
        log.info("获取用户信息, userId:{}", userId);
        return userService.getUserInfo(userId);
    }

    /**
     * 获取博客的作者信息
     *
     * @param blogId 博客ID，不能为空
     * @return 博客作者的信息响应对象
     */
    @GetMapping("/getAuthorInfo")
    @Operation(summary = "获取博客作者信息", description = "根据博客ID获取该博客的作者信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证"),
        @ApiResponse(responseCode = "404", description = "博客不存在")
    })
    public UserInfoResponse getAuthorInfo(@Parameter(description = "博客ID") @NotNull Integer blogId) {
        log.info("获取博客的作者信息, blogId:" + blogId);
        return userService.getAuthorInfo(blogId);
    }
    
    /**
     * 获取所有用户列表
     *
     * @return 用户信息响应列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取用户列表", description = "获取所有未删除的用户列表，按更新时间倒序排列")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "401", description = "未认证")
    })
    public List<UserInfoResponse> getAllUsers() {
        log.info("获取用户列表");
        return userService.getAllUsers();
    }
    
    /**
     * 获取用户详情
     *
     * @param userId 用户ID，不能为空
     * @return 用户信息响应对象
     */
    @GetMapping("/detail")
    @Operation(summary = "获取用户详情", description = "根据用户ID获取用户详细信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证"),
        @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public UserInfoResponse getUserDetail(@Parameter(description = "用户ID") @NotNull Integer userId) {
        log.info("获取用户详情, userId:{}", userId);
        return userService.getUserDetail(userId);
    }
    
    /**
     * 创建新用户
     *
     * @param createUserRequest 创建用户请求对象，包含用户名、密码等信息
     * @return 是否创建成功
     */
    @PostMapping("/create")
    @Operation(summary = "创建用户", description = "创建新的用户账户")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "创建成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "409", description = "用户已存在")
    })
    public Boolean createUser(@Valid @RequestBody CreateUserRequest createUserRequest) {
        log.info("创建用户, request:{}", createUserRequest);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(createUserRequest.getUserName());
        userInfo.setPassword(createUserRequest.getPassword());
        userInfo.setGithubUrl(createUserRequest.getGithubUrl());
        return userService.createUser(userInfo);
    }
    
    /**
     * 更新用户信息
     *
     * @param updateUserRequest 更新用户请求对象，包含要更新的用户信息
     * @return 是否更新成功
     */
    @PostMapping("/update")
    @Operation(summary = "更新用户", description = "更新已存在的用户信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "更新成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证"),
        @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public Boolean updateUser(@Valid @RequestBody UpdateUserRequest updateUserRequest) {
        log.info("更新用户, request:{}", updateUserRequest);
        UserInfo userInfo = new UserInfo();
        userInfo.setId(updateUserRequest.getId());
        userInfo.setUserName(updateUserRequest.getUserName());
        userInfo.setGithubUrl(updateUserRequest.getGithubUrl());
        return userService.updateUser(userInfo);
    }
    
    /**
     * 删除用户（逻辑删除）
     *
     * @param userId 要删除的用户ID
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    @Operation(summary = "删除用户", description = "逻辑删除指定的用户")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "删除成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证"),
        @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public Boolean deleteUser(@Parameter(description = "用户ID") @NotNull Integer userId) {
        log.info("删除用户, userId:{}", userId);
        return userService.deleteUser(userId);
    }
}